package com.xqx.metool.controller;


import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.xqx.metool.service.HttpService;
import com.xqx.metool.util.SqlBuildUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Author : xingquanxiang
 * @Date : 2022/1/1 23:58
 */
@RestController
public class TestController {
    public static final String url = "http://localhost:9999/test2";

    @Autowired
    private HttpService httpService;

    @RequestMapping ("/index")
    public Object test1(String name) {
        return "AA" + name;
    }

    @RequestMapping ("/test2")
    public Object test2(String sql, String params, HttpServletRequest request) {
        System.out.println("sql = " + sql);
        System.out.println("params = " + params);
        return "sql: " + sql + "  params：" + params;
    }

    @RequestMapping (value = "/build/sql", method = RequestMethod.POST)
    public Object buildSql(String sql, String params) {
        return SqlBuildUtil.sqlBuild(sql, params);
    }

    @RequestMapping (value = "/build/normal/sql", method = RequestMethod.POST)
    public Object buildNormalSql(String sql, String params) {
        return SqlBuildUtil.sqlNormalBuild(sql, params);
    }


    @RequestMapping ("/build/sql/cookie")
    public Object testHttp(String sql, String params, String cookie) {
        HashMap<String, String> header = new HashMap<>();
        HashMap<String, Object> paramMap = new HashMap<>();

        header.put("Cookie", cookie);

        paramMap.put("sql", sql);
        paramMap.put("params", params);
        return httpService.doPost(url, paramMap, header);
    }

    @RequestMapping("/collection/intersection")
    public Object testIntersection(String configIds, String grayIds){
        HashSet<String> configIdSet = Sets.newHashSet(configIds.split(","));
        HashSet<String> grayIdSet = Sets.newHashSet(grayIds.split(","));
        return CollectionUtils.intersection(Lists.newArrayList(configIdSet), Lists.newArrayList(grayIdSet));
    }
}
